package com.yunxi.estate.config.interceptor;

import com.yunxi.estate.domain.SysUser;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器
 *
 * @author 松子鱼
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        SysUser sysUser = (SysUser) request.getSession().getAttribute("user");
        if (sysUser == null) {
            String urlString = request.getRequestURI();
            if (urlString.endsWith("login")) {
                return true;
            }
            response.sendRedirect(request.getContextPath() + "/login");
        }
        return true;
    }
}
